﻿namespace HZY.Framework.Core.Redis.Impl;

/// <summary>
/// RedisServiceImpl
/// </summary>
public class RedisServiceImpl : IRedisService
{
    /// <summary>
    /// get database
    /// </summary>
    public IDatabase Database { get; }

    /// <summary>
    /// 
    /// </summary>
    public IConnectionMultiplexer Multiplexer { get; }

    public RedisServiceImpl(string connectionString)
    {
        Multiplexer = ConnectionMultiplexer.Connect(connectionString, opt =>
        {
            // 自定义证书验证逻辑 // 接受所有证书
            opt.CertificateValidation += (sender, certificate, chain, sslPolicyErrors) => true;
        });
        this.Database = Multiplexer.GetDatabase();
    }

    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // Cleanup
            this.Multiplexer?.Close();
            this.Multiplexer?.Dispose();
        }
    }

    ~RedisServiceImpl()
    {
        Dispose(false);
    }
}